<template>
  <div class="w-[100%] h-[100%]">

    <div class="">
      <ContentWrap>
        <SearchBox :queryForm="searchList" :showExpland="true" @query="handleSearchQuery"
                   @changeVal="onChange"
        />
      </ContentWrap>
    </div>

    <div class="mt-[30px]">
      <QueryTable
        :list="dataList"
        :total="total"
        :btns="tableBtnList"
        @execute="handleBtnClick"
        v-model="queryParams"
        :thead="th"
        :show-action="false"
        :show-section="false"
      >
        <template #createTime="{row}">
          {{ dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss') }}
        </template>

        <template #action="{row}">
          <div class="text-line2" :title="row.action">{{ row.action }}</div>
        </template>
      </QueryTable>

    </div>
  </div>
</template>
<script setup lang="ts">
import Icon from "@/components/Icon/src/Icon.vue"
import QueryTable from "@/components/TableData/index.vue"
import ImagePreview from "@/components/ImagePreview/index.vue"
import type {TradeOrderQueryVO} from "@/api/mall/trade/order";
import {DICT_TYPE, getIntDictOptions} from "@/utils/dict";
import type {PurchaseAdvice} from "@/api/purchase/advice/type";
import {tify, sify, tifyJson} from 'chinese-conv'
import dayjs from "dayjs";

import {getLogPage, OperateLogRespVO} from "@/api/setting/log";
import {GlobeStateApi} from "@/api/logistics/globestate";
import {getSimpleDeptList} from "@/api/system/dept";
import {getUserPageNew} from "@/api/member/user";

const searchList: TradeOrderQueryVO[] = ref(
  [
    {
      label: '部门',
      code: 'deptId',
      type: 'select',
      options: []
    },

    {
      label: '业务员',
      code: 'userId',
      type: 'select',
      options: []
    },
    {
      label: '功能菜单',
      code: 'subType',
      type: 'input'
    },
    {
      label: '操作动作',
      code: 'action',
      type: 'input'
    },
  ]
)


const initQueryParams = {
  pageNo: 1, pageSize: 10
}


const th = ref([
  {
    name: '部门',
    code: 'deptName',
    type: 'numberRanger',
    search: true
  },
  {
    name: '用户名称',
    code: 'userName',
    type: 'date',
    search: true
  },
  {
    name: '功能菜单',
    code: 'subType',
    search: false
  },
  {
    name: '操作动作',
    code: 'action',
    type: 'numberRanger',
    search: true
  },
  {
    name: '操作端',
    code: 'loginEnd',
    search: false
  },
  {
    name: 'IP/归属',
    code: 'userIp',
    search: false
  },
  {
    name: '操作时间',
    code: 'createTime',
    date: true,
  },
]);
const dataList = ref<OperateLogRespVO[]>([]);
const ids = ref<Array<string | number>>([]);
const total = ref(0);

const initRow = {}

const selectedRow = ref<OperateLogRespVO>({...initRow})

const isShowDialog = ref(false)


const queryParams = ref({
  ...initQueryParams
})

const handleSearchQuery = (val) => {
  /***
   * 搜索框查询条件，目前考虑在查询时 queryParams = {...val,pageNo:1,pageSize:10}
   * */
  queryParams.value = {
    ...initQueryParams,
    ...val
  }
  queryList();
}

const handleBtnClick = e => {
  let type = e.type;
  try {
    fn[type](e)
  } catch (e) {
    console.log(`${type}不存在`)
  }
}

const fn = {
  link({data}) {
    selectedRow.value = data.row;
    isShowDialog.value = true
  },
  pagination() {
    queryList();
  }
}

const queryList = async () => {
  let res = await getLogPage(queryParams.value);
  dataList.value = res.list;
  total.value = res.total;
}

const initPromise = async () => {
  let res = await Promise.all([getSimpleDeptList()])
  console.log("66666")
  //获取部门
  let item = searchList.value.find(it => {
    return it.code == "deptId"
  })
  console.log(item, "====iiii")
  item.options = res[0]?.map(item => {
    return {
      label: item.name,
      value: item.id
    }
  })


}

const onChange = ({item, form}) => {
  if (item.code == "deptId") {
    getUser(form.deptId);
  }
}


/**
 * 根据部门获取
 * */
const getUser = async (deptId) => {
  let res = await getUserPageNew({
    deptId
  });

  let item = searchList.value.find(at => {
    return at.code == "userId"
  })
  item.options = (res || []).map(item => {
    return {
      label: item.nickname,
      value: item.id
    }
  });
}


onMounted(() => {
  // const chsAsObject = {
  //   text1: '所谓知己知彼百战不殆',
  //   text2: '作为星际2职业选手',
  //   text3: '他们在平时练习中不仅要练好自己的本族',
  //   text4: '还会经常选择其他两个族进行练习',
  // }
  //
  // let cb = tifyJson(chsAsObject)
  // console.log(cb)

  queryList();

  initPromise();

})

</script>
<style lang="scss" scoped>

</style>
